package com.mymoney.ws.asynctasks;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;

import com.mymoney.MyMoneyActivity;
import com.mymoney.constants.Constants;
import com.mymoney.constants.RequestPaths;
import com.mymoney.data.access.JsonParser;
import com.mymoney.exceptions.CommunicationException;
import com.mymoney.helpers.AppStatus;
import com.mymoney.helpers.DeviceInfoHelper;
import com.mymoney.ws.client.SSLHttpClient;
import com.mymoney.ws.data.DeviceInformation;
import com.mymoney.ws.data.RegisterDeviceResponse;

/**
 * AsyncTask that call RegisterDevice web service
 *
 * @author dimitar.zlatkov
 */
public abstract class RegisterDeviceAsyncTask extends AsyncTask<String, Void, Void> {

    private static String TAG = "RegisterDeviceAsyncTask";
    private Context context;

	public RegisterDeviceAsyncTask(MyMoneyActivity context) {
        this.context = context;
    }

    @Override
    protected Void doInBackground(String... arg0) 
    {
    	if(!AppStatus.isAccountAnnalized)
    	{
	        Log.i(TAG, "RegisterDeviceAsyncTask on background start...");
	        SSLHttpClient httpClient = new SSLHttpClient(context);
	
	        DeviceInfoHelper deviceInfoHelper = new DeviceInfoHelper(context);
	
	        String platformName;
	        if (deviceInfoHelper.isTablet()) {
	            platformName = Constants.TABLET_PLATFORM_CODE;
	        } else if (deviceInfoHelper.isLargePhone((Activity) context)) {
	            platformName = Constants.LARGE_PHONE_PLATFORM_CODE;
	        } else {
	            platformName = Constants.SMALL_PHONE_PLATFORM_CODE;
	        }
	
	        String path = RequestPaths.getInstance().createRegisterDeviceRequestPath(platformName);
	        DeviceInformation deviceInformation = deviceInfoHelper.getDeviceInformation();
	        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
	        String requestBody = JsonParser.getInstance().toJSON(deviceInformation);
	        try {
	            String responceBody = httpClient.post(path, requestBody);
	            RegisterDeviceResponse deviceResponseBean = JsonParser.getInstance().getRegisterDeviceBean(responceBody);
	            SharedPreferences.Editor prefsEditor = sharedPrefs.edit();
	            if(deviceResponseBean != null)
	            {
		            prefsEditor.putString(Constants.DEVICE_ID_KEY, deviceResponseBean.getId());
		            prefsEditor.commit();
	            }
	        } 
	        catch (CommunicationException e) {
	        	Log.e(TAG, "Immediate internal error", e);
			}
	        catch (Exception e) {
	            Log.e(TAG, "Immediate internal error", e);
	        }
	        AppStatus.isAccountAnnalized = true;
    	}
            
            return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        onFinish();
    }

    public abstract void onFinish();
}
